import { Injectable } from '@nestjs/common';
import { CreateApEphDto } from './dto/create-ap_eph.dto';
import { UpdateApEphDto } from './dto/update-ap_eph.dto';
import { resolve } from 'path';

@Injectable()
export class ApEphService {
  create(createApEphDto: CreateApEphDto) {
    return 'This action adds a new apEph';
  }
  public codeMessage = {
    code: 200,
    message: '获取成功',
  };
  public ephemerisInfo = [
    {
      id: 1,
      sputnik: 'string',
      earth_station: 'kuerle1',
      date: Date.now(),
      eccentricity_rate: 'string',
      semi_major_axis: 'string',
      dip: 'string',
      right_ascension_ascende_node: 'string',
      argument_perigee: 'string',
      mean_anoma: 'string',
      longitude: '0',
      latitude: '0',
      height: '0',
      visual_angle: '0',
    },
    {
      id: 2,
      sputnik: 'string',
      earth_station: 'kuerle1',
      date: Date.now(),
      eccentricity_rate: 'string',
      semi_major_axis: 'string',
      dip: 'string',
      right_ascension_ascende_node: 'string',
      argument_perigee: 'string',
      mean_anoma: 'string',
      longitude: '0',
      latitude: '0',
      height: '0',
      visual_angle: '0',
    },
  ];
  getEphemerisInfo() {
    return [
      {
        id: 1,
        sputnik: 'string',
        earth_station: 'kuerle1',
        date: Date.now(),
        eccentricity_rate: 'string',
        semi_major_axis: 'string',
        dip: 'string',
        right_ascension_ascende_node: 'string',
        argument_perigee: 'string',
        mean_anoma: 'string',
        longitude: '0',
        latitude: '0',
        height: '0',
        visual_angle: '0',
      },
      {
        id: 2,
        sputnik: 'string',
        earth_station: 'kuerle1',
        date: Date.now(),
        eccentricity_rate: 'string',
        semi_major_axis: 'string',
        dip: 'string',
        right_ascension_ascende_node: 'string',
        argument_perigee: 'string',
        mean_anoma: 'string',
        longitude: '0',
        latitude: '0',
        height: '0',
        visual_angle: '0',
      },
    ];
  }
  ephemeris_get() {
    return {
      code: 200,
      message: '获取成功',
      data: {
        list: this.ephemerisInfo,
      },
    };
  }
  async ephemeris_cal(body: any) {
    await new Promise((resolve) => setTimeout(resolve, 2000));
    return {
      code: 200,
      message: '计算成功',
      data: body,
    };
  }

  async ephemeris_set(body: any) {
    const index = this.ephemerisInfo.findIndex((item) => item.id === body.id);
    if (index !== -1) {
      this.ephemerisInfo[index] = body;
    } else {
      body.id = this.ephemerisInfo.length + 1;
      this.ephemerisInfo.push(body);
    }
    return {
      code: 200,
      message: '设置成功',
      data: body,
    };
  }

  private ephemerisRecordData = [
    {
      id: 0,
      name: 'string',
      trace_begin: Date.now(),
      trace_end: Date.now() + 1000 * 60 * 60 * 24,
      elevate_max: 20,
    },
    {
      id: 1,
      name: 'string',
      trace_begin: Date.now(),
      trace_end: Date.now() + 1000 * 60 * 60 * 24,
      elevate_max: 50,
    },
  ];
  ephemeris_record() {
    return {
      code: 200,
      message: 'success',
      data: {
        record: this.ephemerisRecordData,
        send_status: '111',
        cal_status: '222',
      },
    };
  }

  ephemeris_ctrl(body: any) {
    let data = '';
    if (body.command === 'detail') {
      data = 'http://192.168.5.74:3001/public/app.log';
    }
    return {
      code: 200,
      message: 'success',
      data: {
        data,
      },
    };
  }

  async ephemeris_delete(id: number) {
    const index = this.ephemerisInfo.findIndex((item) => item.id === id);
    if (index !== -1) {
      this.ephemerisInfo.splice(index, 1);
    }
    return {
      code: 200,
      message: '删除成功',
    };
  }

  private twelve_ephemeris_data = {
    starId: '19',
    epochTime: Date.now(),
    semiMajorAxis: '-61057.903',
    semiMajorAxisChangeRate: '-0.00826397255',
    eccentricityRate: '0.00119537052',
    eccentricityRateChangeRate: '-4.66737185E-14',
    inclination: '7.63128568E-6',
    inclinationChangeRate: '1.1092685300000001E-11',
    ascendNodeRa: '0.142477724',
    perigeeArg: '0.500394245',
    meanAnomaly: '-0.535348041',
    ascendNodeRaChangeRate: '-2.22440588E-8',
    perigeeArgChangeRate: '0.0',
    meanAnomalyChangeRate: '-1.52369362E-27',
    longitude: '122',
    latitude: '12',
    height: '12',
    startTime: '2025-11-10 17:35:25',
    forecastDuration: '24',
    satelliteType: '0',
  };

  twelve_ephemeris_get() {
    return {
      code: 200,
      message: 'success',
      data: this.twelve_ephemeris_data,
    };
  }

  private sib2_data = {
    sib2: 0,
  };

  sib2_set(body: any) {
    this.sib2_data.sib2 = body.sib2;
    return {
      code: 200,
      message: 'success',
    };
  }

  sib2_get() {
    return {
      code: 200,
      message: 'success',
      data: this.sib2_data,
    };
  }

  ephemeris_tlw_set(body: any) {
    return {
      code: 200,
      message: 'success',
    };
  }
}
